package com.undsf.despot;

import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.g2d.BitmapFont;
import com.badlogic.gdx.graphics.g2d.freetype.FreeTypeFontGenerator;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Arathi on 2016/10/14.
 */
public class ResourceCache {
    private static ResourceCache instance;
    public Map<String, Object> cached;

    public static ResourceCache GetInstance() {
        if (instance == null) {
            instance = new ResourceCache();
        }
        return instance;
    }

    private ResourceCache() {
        cached = new HashMap<String, Object>();
        initFonts();
    }

    public void initFonts() {
        FreeTypeFontGenerator generator = new FreeTypeFontGenerator(Gdx.files.internal("fonts/simsun.ttc"));
        FreeTypeFontGenerator.FreeTypeFontParameter parameter = new FreeTypeFontGenerator.FreeTypeFontParameter();
        parameter.size = 12;
        StringBuffer sb = new StringBuffer();
        sb.append(FreeTypeFontGenerator.DEFAULT_CHARS);
        for (char c=0x4E00; c<=0x9FA5; c++) {
            sb.append(c);
        }
        parameter.characters = sb.toString();
        BitmapFont bmpFont12 = generator.generateFont(parameter);
        generator.dispose();

        cached.put("bmpFont12", bmpFont12);
    }

    public Object getResourceByName(String name) {
        Object res = cached.get(name);
        return res;
    }
}
